#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <fstream>
#include <stdint.h>
#include <stdio.h>
#include <iconv.h>
#include <cerrno>
#include <cstring>
#include <unordered_set>
#include <vector>

//static int s_iGtt808TerminalModelLen = 31;

static void ConvectErrorFormatData(const std::string& strData, std::string& strResult)
{
    if (strData.empty())
    {
        strResult.clear();
        return;
    }
    strResult.clear();
    strResult.reserve(strData.length());
    for (const unsigned char c : strData)
    {
        switch (c)
        {
        case '\r':
        {
            strResult += "\\r";
            break;
        }
        case '\n':
        {
            strResult += "\\n";
            break;
        }
        case '\v':
        {
            strResult += "\\v";
            break;
        }
        case '\f':
        {
            strResult += "\\f";
            break;
        }
        case '|':
        {
            strResult += "%";
            break;
        }
        default:
        {
            strResult += static_cast<char>(c);
            break;
        }
        }
    }
}

static const std::unordered_set<std::string> kProvincesUTF8 = {
    "京", "津", "沪", "渝", "冀", "豫", "云", "辽",
    "黑", "湘", "皖", "鲁", "新", "苏", "浙", "赣",
    "鄂", "桂", "甘", "晋", "蒙", "陕", "吉", "贵",
    "青", "藏", "川", "宁", "琼", "粤", "闽", "港", "澳"
};

std::string GBKToUTF8(const std::string &gbkStr)
{
    if (gbkStr.empty())
    {
        return "";
    }

    iconv_t cd = iconv_open("UTF-8", "GBK");
    if (cd == (iconv_t)-1)
    {
        // 转换器创建失败，返回原字符串
        return gbkStr;
    }

    size_t inLen = gbkStr.size();
    size_t outLen = inLen * 3; // UTF-8 汉字最多3字节
    std::vector<char> outBuf(outLen);

    auto *inPtr = (char*)gbkStr.c_str();
    char *outPtr = outBuf.data();
    size_t outLenRemain = outLen;

    size_t result = iconv(cd, &inPtr, &inLen, &outPtr, &outLenRemain);
    iconv_close(cd);

    if (result == (size_t)-1)
    {
        // 转换失败，可能原本就是UTF-8，返回原字符串
        return gbkStr;
    }

    return std::string(outBuf.data(), outLen - outLenRemain);
}

int IsPlatePrefixUTF8(const std::string& plate) 
{
    if (plate.size() < 4) 
    {
        return 0;  // UTF-8: 汉字3字节 + 字母1字节，至少4字节
    }
    
    // 提取前3个字节（UTF-8单个汉字）
    std::cout << "plate: " << plate << std::endl;
    std::string firstChar = plate.substr(0, 3);
    std::cout << "firstChar: " << firstChar << " " << firstChar.size() << std::endl;
    // 检查是否是合法省份
    if (kProvincesUTF8.count(firstChar) == 0) 
    {
        return 0;
    }
    
    // 第4个字节应该是大写字母
    auto c = static_cast<unsigned char>(plate[3]);
    if (!std::isupper(c)) 
    {
        return 0;
    }
    
    return 1;
}


int IsPlateValid(std::string& plate) 
{
    if (plate.empty()) 
    {
        return 0;
    }
    
    // 步骤1: 统一转换为 UTF-8
    plate = GBKToUTF8(plate);
    
    // 步骤2: 用 UTF-8 规则检测车牌规范
    return IsPlatePrefixUTF8(plate);
}




int main()
{
    // const char* temp = "Hello 0x1F World";
    // std::cout << temp << std::endl;
    // uint8_t TerminalModel[31] = {0xc8, 0xbc, 0x2c, 0x01, 0xA, 0xc2, 0x81, 0xA};

    // std::string strData((char*)TerminalModel);
    // std::string result = "";
    // ConvectErrorFormatData(strData, result);
    // auto len = std::min(s_iGtt808TerminalModelLen - 1, (int)result.length());
    // memset(TerminalModel, 0, len);
    // memcpy(TerminalModel, result.c_str(), len);
    // std::cout << TerminalModel;
    // TerminalModel[len] = '\0';

    for (auto temp : kProvincesUTF8)
    {
        std::cout << "temp " << temp.size() << std::endl;
    }

    std::string temp = "\xbe\xa9";
    std::string plateGBK =  temp + "A12345";
    std::string plateUTF8 = GBKToUTF8(plateGBK);
    std::cout << IsPlatePrefixUTF8(plateUTF8) << std::endl;

    return 0;
}
